function BasicDic_main() {
    return $.layout({
        id: 'basicDic_main',
        items: [{
            top: [$.treeSearch],
            region: 'west',
            width: 230,
            style: $.border('Right'),
            xtype: 'jtree',
            clazz: BasicDic,
            where: [['dicType', '=', '1']],
            id: 'basic_dic_tree',
            order: ['dicSort->asc'],
            mapper: 'id,dicKey,dicParentId.id,dicValue,iconCls,cls',
            change: [{
                target: 'basicDic_grid',
                targetField: 'dicParentId.id'
            }]
        }, {
            dragSort: {
                sort: 'dicSort',
                before: function (cmp, store) {
                    return $('basic_dic_tree').select().length != 0;
                },
                convert: 'basicDicConvert'
            },
            dClick: function (cmp, r) {
                basicDic_form().view({
                    entityId: r.get('id')
                });
            },
            region: 'center',
            id: 'basicDic_grid',
            xtype: 'jgrid',
            clazz: BasicDic,
            order: ['dicState->desc', 'dicSort->asc'],
            where: [['id', '!in', 'root,all']],
            top: [{
                xtype: 'textfield',
                name: 'dicKey',
                labelWidth: 55,
                op: 'like'
            }, {
                xtype: 'textfield',
                text: '字典值',
                name: 'dicValue',
                labelWidth: 55,
                op: 'like'
            }, $.search, '->', {
                tooltip: '添加字典',
                iconCls: ion.add,
                handler: function () {
                    basicDic_form().save({
                        before: function (form) {
                            var select = $('basic_dic_tree').select()[0];
                            if (select) {
                                form.set('dicType', select.get('id') == 'all' ? '1' : '0');
                            }
                        },
                        rk: {
                            msg: '选中一个字典分组后才能进行添加',
                            id: 'basic_dic_tree',
                            exp: ['text->dicParentId.dicKey', 'id->dicParentId.id']
                        },
                        grid: ['basicDic_grid', 'basic_dic_tree']
                    });
                }
            }],
            columns: [{
                maxWidth: 40,
                text: '#',
                name: 'dicSort'
            }, {
                width: 120,
                text: '所属分组',
                name: 'dicParentId.dicKey',
                renderer: function (v, m) {
                    m.tdCls = 'td-gray';
                    return v;
                }
            }, {
                dic: 'state',
                name: 'dicState',
                renderer: $.dicRenderer('state', function (v, m, r) {
                    m.tdCls = v == '1' ? 'td-green' : 'td-red';
                })
            }, {
                width: 150,
                text: '字典键',
                name: 'dicKey'
            }, {
                width: 120,
                text: '字典值',
                name: 'dicValue'
            }, {
                flex: 1,
                text: '允许编辑',
                name: 'allowEdit',
                dic: 'yesNo',
                renderer: $.yesNoRenderer()
            }, {
                xtype: 'actioncolumn',
                width: 100,
                items: [{
                    tooltip: '修改',
                    iconCls: 'fa-pencil-square td-yellow x-fa',
                    handler: function (grid, rowIndex, colIndex) {
                        var cmp = $('basicDic_grid'), rec = grid.getStore().getAt(rowIndex);
                        cmp.select(rec);
                        CLICK_TEXT = '修改字典';
                        basicDic_form().edit({
                            readOnlys: dicAllowEdit(),
                            grid: ['basicDic_grid', 'basic_dic_tree']
                        });
                    }
                }, {
                    tooltip: '删除',
                    iconCls: 'fa-close td-red x-fa',
                    handler: function (grid, rowIndex, colIndex) {
                        var cmp = $('basicDic_grid'), rec = grid.getStore().getAt(rowIndex);
                        cmp.select(rec);
                        cmp.deletes({
                            grid: ['basic_dic_tree'],
                            before: function () {
                                if (dicAllowEdit().length > 0) {
                                    return $.error('该字典不允许编辑,无法删除');
                                }
                            },
                            validate: [[BasicDic, 'dicParentId.id', '字典键为{dicKey}的字典存在子数据,清除子数据后才能删除.']]
                        });
                    }
                }]
            }]
        }]
    })
}

function basicDic_form() {
    return $({
        xtype: 'jform',
        clazz: BasicDic,
        fieldsSize: 0.5,
        hides: ['dicParentId.id'],
        validate: [['dicParentId.id = ?,dicKey = ?', 'dicKey', '同一字典分组下字典键不能重复'],
            ['dicParentId.id = ?,dicValue = ?', 'dicValue', '同一字典分组下字典值不能重复']],
        fields: [{
            name: 'dicParentId.dicKey',
            text: '所属分组',
            readOnly: true
        }, {
            req: true,
            name: 'dicType',
            text: '字典类型',
            xtype: 'combo',
            store: $.dic('dicType'),
            readOnly: true
        }, {
            req: true,
            maxLength: 40,
            name: 'dicKey',
            text: '字典键'
        }, {
            req: true,
            maxLength: 99,
            name: 'dicValue',
            text: '字典值'
        }, {
            name: 'allowEdit',
            text: '允许编辑',
            value: '1',
            xtype: 'combo',
            store: $.dic('allowEdit'),
            req: true
        }, {
            name: 'dicState',
            text: '字典状态',
            xtype: 'combo',
            value: '1',
            store: $.dic('state'),
            req: true
        }, {
            minValue: 0,
            value: 0,
            maxLength: 9,
            name: 'dicSort',
            xtype: 'numberfield'
        }, {
            size: 1,
            name: 'dicMemo',
            xtype: 'textarea',
            text: '字典备注'
        }]
    })
}

// 判断该字典是否允许编辑
function dicAllowEdit() {
    var sel = $('basicDic_grid').select()[0];
    if (sel) {
        if (sel.get('allowEdit') == '0') {
            return ['dicKey', 'dicValue'];
        }
    }
    return [];
}